package com.books.controller.search;

import com.books.model.entity.book.Books;
import com.books.model.entity.book.Ebook;
import com.books.service.book.IBookService;
import com.books.service.book.IEbookService;
import com.books.service.searchimg.FindAllFile;
import com.books.service.searchimg.LikePicture;
import com.books.service.searchimg.MultipartFileToFile;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 饿熊的咆哮
 * @Date 2021/10/27 18:07
 * @Version 1.0
 */
@Controller
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private IBookService bookService;

    @Autowired
    private IEbookService ebookService;
    
    @GetMapping("/search")
    public String search(){
        return "search/Search";
    }

    @PostMapping("/findImgBook")
    @ResponseBody
    public JSONArray findImgBook(MultipartFile file, HttpServletRequest request){
        List<String> highLike = new ArrayList<>();
        String path = request.getServletContext().getRealPath("/");
        File[] files = FindAllFile.findAllFile(path+"bookImg");
        File upImg = null;
        try {
            upImg = MultipartFileToFile.multipartFileToFile(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (File file1 : files) {
            Double like = null;
            try {
                like = LikePicture.imgLike(upImg,file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (like>=0.7){
                String a = file1.getName();
                int index = a.indexOf(".");
                String str2 = a.substring(0,index);
                highLike.add(str2);
            }
        }
        List<Books> books = new ArrayList<>();
        for (String s : highLike) {
            Integer bkid = Integer.valueOf(s);
            Books book = bookService.getById(bkid);
            books.add(book);
        }
        JSONArray jsonArray = JSONArray.fromObject(books);
        return jsonArray;
    }

    @PostMapping("/findImgEBook")
    @ResponseBody
    public JSONArray findImgEBook(MultipartFile file, HttpServletRequest request){
        List<String> highLike = new ArrayList<>();
        String path = request.getServletContext().getRealPath("/");
        File[] files = FindAllFile.findAllFile(path+"ebookImg");
        File upImg = null;
        try {
            upImg = MultipartFileToFile.multipartFileToFile(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (File file1 : files) {
            Double like = null;
            try {
                like = LikePicture.imgLike(upImg,file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (like>=0.7){
                String a = file1.getName();
                int index = a.indexOf(".");
                String str2 = a.substring(0,index);
                highLike.add(str2);
            }
        }
        List<Ebook> ebooks = new ArrayList<>();
        for (String s : highLike) {
            Integer ebkid = Integer.valueOf(s);
            Ebook ebook = ebookService.getById(ebkid);
            ebooks.add(ebook);
        }
        JSONArray jsonArray = JSONArray.fromObject(ebooks);
        return jsonArray;
    }
}
